package com.hy.mvc.biz.service.common.support;

import com.hy.mvc.domain.dataobject.base.Bean;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.List;

/**
 * <pre>
 *    @author  : Booker
 *    time    : 2017/12/14 19:27
 *    desc    : save the page of context
 *    version : v1.0
 * </pre>
 */
public class PageContext implements Serializable {

    public static final String USER = "user";

    private static ThreadLocal<PageContext> currentPageContext = new ThreadLocal<PageContext>() {
        @Override
        protected PageContext initialValue() {
            return new PageContext();
        }
    };
    private final Logger logger = LoggerFactory.getLogger(getClass());

    private HttpServletRequest request;

    private HttpServletResponse response;

    private HttpSession session;

    public static ThreadLocal<PageContext> getCurrentPageContext() {
        return currentPageContext;
    }

    public static PageContext current() {
        return currentPageContext.get();
    }

    public Object getAttribute(String paramName) {
        HttpServletRequest req = getRequest();
        return req.getAttribute(paramName);
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public HttpSession getSession() {
        return request.getSession();
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }

    public User getUser() {
        return (User) request.getAttribute(USER);
    }

    public void setBean(String name, Bean bean) {
        request.setAttribute(name, bean);
    }

    /**
     * 将一组bean设置到request中
     *
     * @param beanListName
     * @param beanList
     */
    public void setBeanList(String beanListName, List<? extends Bean> beanList) {
        request.setAttribute(beanListName, beanList);
    }

    /**
     * 设置msg页面的提示信息
     *
     * @param msg
     */
    public void setMsg(String msg) {
        request.setAttribute("msg", msg);
    }

    /**
     * @param paramName
     * @param defaultValue
     * @return
     */
    public long getLongValue(String paramName, long defaultValue) {
        String paramValue = request.getParameter(paramName);
        if (StringUtils.isBlank(paramValue)) {
            return defaultValue;
        } else {
            paramValue = paramValue.trim();
            try {
                return Long.parseLong(paramValue);
            } catch (NumberFormatException e) {
                logger.error(paramValue + " convert to long fail ", e);
                return defaultValue;
            }
        }

    }

    /**
     * 得到用户取请求的url后缀
     *
     * @return
     */
    public String getRequestUrl() {
        String url = request.getRequestURI() + "?" + request.getQueryString();
        return url;
    }

    /**
     * 设置自动跳转的页面的timeout
     *
     * @param timeout
     */
    public void setTimeout(int timeout) {
        request.setAttribute("timeout", timeout);
    }

    /**
     * 将属性设定到request中
     *
     * @param name
     * @param value
     */
    public void setAttribute(String name, java.lang.Object value) {
        request.setAttribute(name, value);
    }

    /**
     * 静态内部类
     */
    @Data
    public static class User implements Bean {

        private String userName;

        private List<Integer> bits;
    }
}

